home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / k_r.arc / K&R_CH4.C < prev    next >
Text File  |  1985-11-05  |  7KB  |  597 lines

  1. pr 4.*
  2.  
  3.  
  4. Jul 27 17:12 1984  4.08getdbl.c Page 1
  5.  
  6.  
  7. #define EOF -1
  8. int main()      /* obtain 3 dimensions and compute volume */
  9. {       double l, w, h, getdbl(), vol();
  10.  
  11.         l = getdbl(); w = getdbl(); h = getdbl();
  12.         printf("(dimensions %.2f x %.2f x %.2f) vol is %.2f\n",
  13.                 l, w, h, vol(l, w, h));
  14. }
  15. double vol(a, b, c)     /* given 3 dimensions compute volume */
  16. double a, b, c;         /* arg declarations */
  17. {
  18.         return (a * b * c);
  19. }
  20. double  getdbl()        /* convert ASCII from terminal to dbl */
  21. {       int     c, sign = 1;    /* c = char input, sign = + */
  22.         float   d = 1.0;        /* d = decimal place */
  23.         double  num = 0.0;      /* num = converted number */
  24.         printf("input number: ");
  25.         if      ((c = getchar()) == '-')
  26.                 sign = -1;                      /* sign = - */
  27.         else if (c == '\n' || c == EOF)
  28.                 return (0);
  29.         else if (c == '.')
  30.                 goto dot;
  31.         else if (c >= '0' && c <= '9')
  32.                 num = (c - '0');
  33.         while ((c = getchar()) != '\n' && c != '.' && c != EOF)
  34.                 if (c >= '0' && c <= '9')
  35.                         num = num * 10 + (c - '0');
  36.         if (c == '.')
  37. dot:            while ((c = getchar()) != EOF && c != '\n')
  38.                         if (c >= '0' && c <= '9')
  39.                         { d /= 10.0; num += d * (c - '0');
  40.                         }
  41.         return (num * sign);
  42. }
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. Jul 27 17:12 1984  4.14vol_a.c Page 1
  71.  
  72.  
  73. double l, w, h, volume; /* external variables */
  74.  
  75. int main()      /* obtain 3 dimensions and compute volume */
  76. {
  77.         double getdbl();
  78.         l = getdbl(); w = getdbl(); h = getdbl();
  79.         vol_ext();
  80.         printf("(dimensions %.2f x %.2f x %.2f) vol is %.2f\n",
  81.                 l, w, h, volume);
  82. }
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. Jul 27 17:12 1984  4.14vol_b.c Page 1
  137.  
  138.  
  139. extern double l, w, h, volume;  /* extern required if sep src file */
  140.  
  141. vol_ext()       /* given 3 dimensions compute volume */
  142. {
  143.         volume = l * w * h;
  144. }
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. Jul 27 17:12 1984  4.14vol_c.c Page 1
  203.  
  204.  
  205. #define EOF -1          /* define moved to this file */
  206. double  getdbl()        /* convert ASCII from terminal to dbl */
  207. {       int     c, sign = 1;    /* c = char input, sign = + */
  208.         float   d = 1.0;        /* d = decimal place */
  209.         double  num = 0.0;      /* num = converted number */
  210.         printf("input number: ");
  211.         if      ((c = getchar()) == '-')
  212.                 sign = -1;                      /* sign = - */
  213.         else if (c == '\n' || c == EOF)
  214.                 return (0);
  215.         else if (c == '.')
  216.                 goto dot;
  217.         else if (c >= '0' && c <= '9')
  218.                 num = (c - '0');
  219.         while ((c = getchar()) != '\n' && c != '.' && c != EOF)
  220.                 if (c >= '0' && c <= '9')
  221.                         num = num * 10 + (c - '0');
  222.         if (c == '.')
  223. dot:            while ((c = getchar()) != EOF && c != '\n')
  224.                         if (c >= '0' && c <= '9')
  225.                         { d /= 10.0; num += d * (c - '0');
  226.                         }
  227.         return (num * sign);
  228. }
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. Jul 27 17:12 1984  4.17perm.c Page 1
  269.  
  270.  
  271. main()  /* test stat() */
  272. {
  273.         int i;
  274.         for (i = 0; i <= 9; ++i)
  275.                 stat();
  276. }
  277. stat()  /* demonstration of internal static */
  278. {
  279.         int fgt = 0;
  280.         static int rem;         /* count calls */
  281.  
  282.         printf("this is call %2d, fgt is %d\n", ++rem, ++fgt);
  283. }
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334. Jul 27 17:12 1984  4.18block.c Page 1
  335.  
  336.  
  337. char ABC[] = { 'A', 'B', 'C', '\0' };                           /* 1st declaration */
  338. main()  /* demonstration of static and scope rules */
  339. { printf("main() ABC[] is %s\n", ABC);
  340.   sub(); sub();
  341. }
  342. sub()   /* declare, initialize, & access two ABC[]'s */
  343. { static int ABC[] = { 1, 2, 3 };                               /* 2nd declaration */
  344.   { static long ABC[] = { 10000, 20000, 30000 };                /* 3rd declaration */
  345.     printf("sub() sub-block ABC[1] is %ld\n", ABC[1]++);
  346.   }
  347.   printf("sub() ABC[1] is %d\n", ABC[1]++);
  348. }
  349.  
  350.  
  351.  
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400. Jul 27 17:12 1984  4.26fact.c Page 1
  401.  
  402.  
  403. /* example of recursive function */
  404.  
  405. main() /* test fact() for range 1 - 10 */
  406. {       register int i;
  407.         double fact();
  408.         for (i = 1; i <= 10; ++i)
  409.                 printf("%2d! is %8.0f\n", i, fact(i));
  410. }
  411. double fact(num)        /* recursive function to compute and
  412.                            return factorial (num!) for num > 0 */
  413. int num;
  414. {       if      (num > 1)
  415.                 return (num * fact(num - 1));
  416.         else    return 1;
  417. }
  418.  
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466. Jul 27 17:12 1984  4.29prepro.c Page 1
  467.  
  468.  
  469. /* examples of #include & #define */
  470.  
  471. #include <stdio.h>      /* EOF - used by getdbl() -
  472.                         /* is defined in <stdio.h> */
  473. #include "getdbl.f"     /* assumes getdbl() is in
  474.                         /* file getdbl.f in wd */
  475. #define max(A, B) ((A) > (B)? (A): (B))
  476.  
  477. main()  /* test max macro */
  478. {
  479.         double getdbl();
  480.         double i = getdbl(), j = getdbl();
  481.         printf("max is %f\n", max(i, j));
  482. }
  483.  
  484.  
  485.  
  486.  
  487.  
  488.  
  489.  
  490.  
  491.  
  492.  
  493.  
  494.  
  495.  
  496.  
  497.  
  498.  
  499.  
  500.  
  501.  
  502.  
  503.  
  504.  
  505.  
  506.  
  507.  
  508.  
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516.  
  517.  
  518.  
  519.  
  520.  
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532. Jul 27 17:12 1984  4.30sqrt.c Page 1
  533.  
  534.  
  535. /* another #include and nested function calls
  536. /* compile as "cc filename -lm"
  537. /* see exp(3M) and start of section 3 (3M) */
  538.  
  539. #include <math.h>       /* declares sqrt() as type double */
  540. #include <stdio.h>      /* provides EOF for getdbl() */
  541. #include "getdbl.f"     /* assumes getdbl() is in
  542.                         /* file getdbl.f in wd */
  543.  
  544. main() /* use sqrt() to compute square roots */
  545. {
  546.         double getdbl();
  547.         printf("square root is %f\n", sqrt(getdbl()));
  548. }
  549.  
  550.  
  551.  
  552.  
  553.  
  554.  
  555.  
  556.  
  557.  
  558.  
  559.  
  560.  
  561.  
  562.  
  563.  
  564.  
  565.  
  566.  
  567.  
  568.  
  569.  
  570.  
  571.  
  572.  
  573.  
  574.  
  575.  
  576.  
  577.  
  578.  
  579.  
  580.  
  581.  
  582.  
  583.  
  584.  
  585.  
  586.  
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.